//
//  SEExamineDetailViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/22.
//

import UIKit

/// ai报告状态 - 0: 未开启或此申请单无眼底检查, 1: 已生成, 2-生成中, 3-未生成
enum SECheckAiStatus: Int {
    case unsupport = 0
    case finish
    case creating
    case waticreate
    case unknown
}

/// 申请单状态 - 1: 待支付, 3: 进行中, 4: 已完成, 5: 已取消
enum SECheckStatus: Int {
    case waitpay = 1
    case ongoing = 3
    case finish = 4
    case cancel = 5
    case unknown

    var title: String {
        switch self {
        case .waitpay: return "待支付"
        case .ongoing: return "进行中"
        case .finish: return "已完成"
        case .cancel: return "已取消"
        default: return "未知状态"
        }
    }

    var titleColor: UIColor {
        switch self {
        case .waitpay: return UIColor(hex: "#F7B500")
        case .ongoing: return UIColor(hex: "#4ABC90")
        default: return Asset.textGrayLightest.color
        }
    }

}

class SEExamineDetailViewModel: NSObject {
    var model = SEExamineModel()

    convenience init(model: SEExamineModel) {
        self.init()
        self.model = model
    }

    var applyFormId: String = ""

    var isAgree: Bool = false

    var payViewModel: SEConfirmReservePayViewModel {
        return SEConfirmReservePayViewModel(price: model.totalFee, time: "", agree: isAgree)
    }

    var fromType: SEExamineOrderViewControllerFromType = .examineReserve

    var updateData: ((SEHttpStatus) -> Void)?
    var payHandler: ((SEHttpStatus) -> Void)?
    var cancelItemHandler: ((SEHttpStatus) -> Void)?
    var commentHandler: ((SEHttpStatus) -> Void)?

    func checkItem(indexPath: IndexPath) -> SEExamineReserveCheckItem {
        return model.checkItemFeeDTOList[indexPath.row]
    }

    var selectedCheckItem = SEExamineReserveCheckItem()

    var checkItemNamesArray: [String] {
        var names: [String] = []
        model.checkItemFeeDTOList.forEach {
            names.append($0.checkItemValue)
        }
        return names
    }

    var checkStatus: SECheckStatus {
        return SECheckStatus(rawValue: model.applyFormFlag) ?? .unknown
    }

    var aiStauts: SECheckAiStatus {
        return SECheckAiStatus(rawValue: model.aiStatus) ?? .unknown
    }

    var reportTitle: String {
        if isHiddenAiLogo {
            return "检查报告"
        } else {
            return "AI检查报告"
        }
    }

    var patientNameDescribe: String {
        return "就诊人: \(model.name)"
    }

    var patientSexDescribe: String {
        return "性别: \(model.sexName)"
    }

    var patientAgeDescribe: String {
        return "年龄: \(model.age)"
    }

    var timeDescribe: String {
        return "预约时间: \(model.checkScheduleTime)"
    }

    var checkTypeDescribe: String {
        return "检查部位: \(model.checkEyePart)"
    }

    var hospitalNameDescribe: String {
        return "检查医院: \(model.checkHosName)"
    }

    func eyeTypeCellModel(indexPath: IndexPath) -> SEExamineOrderEyeTypeCellModel{
        switch indexPath.section {
        case 1:
            return SEExamineOrderEyeTypeCellModel(titleText: "检查眼别", memoText: model.checkEyePart)
        case 3:
            switch indexPath.row {
            case 0:  return SEExamineOrderEyeTypeCellModel(titleText: "就诊人", memoText: model.name)
            case 1:  return SEExamineOrderEyeTypeCellModel(titleText: "服务名称", memoText: "社区检查")
            default: return SEExamineOrderEyeTypeCellModel()
            }
        default: return SEExamineOrderEyeTypeCellModel()
        }
    }

    // MARK: - SEExamineListViewController - 检查记录列表 -

    var isHiddenAiLogo: Bool {
        return !(checkStatus == .finish && aiStauts == .finish)
    }

    var isHiddenAiBtn: Bool {
        return !(checkStatus == .finish && (aiStauts == .creating || aiStauts == .waticreate))
    }

    var isHiddenReportBtn: Bool {
        return checkStatus != .finish
    }

    // - SEExamineListViewController - end -

    // MARK: - SEOrderExamineViewController - 个人中心检查订单列表 -

    var canCancel: Bool {
        return model.canCancel == 1
    }

    var isHiddenCancelBtn: Bool {
        return !canCancel || checkStatus == .unknown
    }

    var isHiddenPayBtn: Bool {
        return checkStatus != .waitpay || checkStatus == .unknown
    }

    var isHiddenDetailBtn: Bool {
        return checkStatus == .waitpay || checkStatus == .unknown
    }

    // - SEOrderExamineViewController - end -

    // MARK: - SEOrderExamineDetailViewController - 个人中心检查订单查看详情 -

    var numberOfSectionForDetail: Int {
        return 5
    }

    func numberOfRowsInSectionForDetail(section: Int) -> Int {
        switch section {
        case 2: return model.checkItemFeeDTOList.count
        case 4:
            switch checkStatus {
            case .waitpay, .cancel, .unknown:
                return 0
            case .ongoing:
                return 1
            case .finish:
                return 2
            }
        default: return 1
        }
    }

    func heightForHeaderForDetail(section: Int) -> CGFloat {
        switch section {
        case 2: return 30
        default: return 0
        }
    }

    func heightForFooterForDetail(section: Int) -> CGFloat {
        switch section {
        case 0, 3: return 10
        default: return 0
        }
    }

    // - SEOrderExamineDetailViewController - end -

    // MARK: - SEExamineOrderViewController - 待支付订单详情页

    var numberOfSectionForWaitPay: Int {
        return 4
    }

    func numberOfRowsInSectionForWaitPay(section: Int) -> Int {
        switch section {
        case 2: return model.checkItemFeeDTOList.count
        case 3: return 2
        default: return 1
        }
    }

    func heightForHeaderForWaitPay(section: Int) -> CGFloat {
        switch section {
        case 1, 3:
            return 28
        case 2:
            return 40
        default:
            return 0
        }
    }

    // - SEExamineOrderViewController - end -


    // MARK: - APIRequest
    /// 订单详情
    func requestExamineDetail() {
        SEHttpManager.shared.request(SEExamineAPI.examineDetail(applyFormId: applyFormId), model: SEExamineModel.self, withLoading: true) { (model, status) in
            switch status {
            case .success:
                self.model = model ?? SEExamineModel()
            default: break
            }
            self.updateData?(status)
        }
    }

    /// 订单支付
    func requestPayExamine() {
        guard isAgree else {
            SEHUD.showError(message: "请先阅读并同意患者须知")
            return
        }
        SEHttpManager.shared.requestForAny(SEPayAPI.pay(id: model.applyOrderNum, payType: 1, payChannel: 1), withLoading: true) { (result, status) in
            self.payHandler?(status)
        }
    }

    /// 取消单项检查
    func requestCancelExamineItem() {
        SEHttpManager.shared.requestForAny(SEExamineAPI.cancelExamineItem(formDetailId: selectedCheckItem.formDetailId), withLoading: true) { (result, status) in
            self.cancelItemHandler?(status)
        }
    }

    /// 检查提交评价
    func submitExamineComment() {
        SEHttpManager.shared.requestForAny(SEExamineAPI.examineSubmitComment(applyFormId: model.id, score: model.score, oldEvaluation: "", isAnonymous: 0), withLoading: true) { result, status in
            self.commentHandler?(status)
        }
    }

}
